package com.yk.faceapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.allinpay.xed.R;
import com.yk.faceapplication.callback.UpdateBillStatusCallback;
import com.yk.faceapplication.entity.Bill;
import com.yk.faceapplication.util.FormatUtil;
import com.yk.faceapplication.util.ImageUtil;

import java.util.List;


public class AccountAdapter extends BaseAdapter {
	private Context context = null;
	private List<Bill> list = null;
	private UpdateBillStatusCallback callback;

	public AccountAdapter(Context context, List<Bill> list,UpdateBillStatusCallback callback) {
		this.context = context;
		this.list = list;
		this.callback = callback;
	}

	@Override
	public int getCount() {
		if (list != null) return list.size();
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (list != null) list.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_account, null);
			viewHolder.prdIconIV = (ImageView) convertView.findViewById(R.id.prd_icon_img);
			viewHolder.prdName = (TextView) convertView.findViewById(R.id.prd_name_tv);
			viewHolder.userName = (TextView) convertView.findViewById(R.id.user_name_tv);
			viewHolder.amountTV = (TextView) convertView.findViewById(R.id.amount_tv);
			viewHolder.timeTV = (TextView) convertView.findViewById(R.id.time_tv);
			viewHolder.settingBtn = (Button) convertView.findViewById(R.id.setting_btn);
			viewHolder.timeFlatTV = (TextView) convertView.findViewById(R.id.time_flag_tv);
			viewHolder.accountNameTV = (TextView) convertView.findViewById(R.id.account_name_tv);
			convertView.setTag(viewHolder);
		}
		else {
			viewHolder = (ViewHolder) convertView.getTag();
		}


		if(list.get(position).getProductIconId() == null ||list.get(position).getProductIconId().equals("")){
			viewHolder.prdIconIV.setImageResource(R.drawable.u62);
        }else{
        	ImageUtil.displayImage(context, list.get(position).getProductIconId(), viewHolder.prdIconIV);
        }

		viewHolder.prdName.setText(list.get(position).getLoanProductName());
		viewHolder.userName.setText(list.get(position).getAccountName());
		viewHolder.amountTV.setText(list.get(position).getLoanAmt());
//		viewHolder.accountNameTV.setText(list.get(position).getBillName());
//        if(list.get(position).getOverDay() ==  0){
//            viewHolder.timeTV.setText("今");
//        }else{
//            viewHolder.timeTV.setText(-list.get(position).getOverDay() + "");
//        }
        viewHolder.timeFlatTV.setText("还款日");
        viewHolder.timeTV.setTextColor(context.getResources().getColor(R.color.textBlack));
        if(list.get(position).getRepaymentDate() == null){
			viewHolder.timeTV.setText("");
		}else{
			viewHolder.timeTV.setText(FormatUtil.getYMD(FormatUtil.long2String(list.get(position).getRepaymentDate())));
		}

//        if(-list.get(position).getOverDay() <= 5){
//            viewHolder.timeTV.setTextColor(context.getResources().getColor(R.color.btn_bg_orange));
//        }else{
//            viewHolder.timeTV.setTextColor(context.getResources().getColor(R.color.textBlack));
//        }
//		if(list.get(position).getOverDay() > 0 ){
//			viewHolder.timeTV.setText(list.get(position).getOverDay() + "");
//			viewHolder.timeFlatTV.setText("逾期日(天)");
//			viewHolder.timeTV.setTextColor(context.getResources().getColor(R.color.red));
//		}else{
//			if(list.get(position).getOverDay() ==  0){
//				viewHolder.timeTV.setText("今");
//			}else{
//				viewHolder.timeTV.setText(-list.get(position).getOverDay() + "");
//			}
//			viewHolder.timeFlatTV.setText("还款日(天)");
//			if(-list.get(position).getOverDay() <= 5){
//				viewHolder.timeTV.setTextColor(context.getResources().getColor(R.color.btn_bg_orange));
//			}else{
//				viewHolder.timeTV.setTextColor(context.getResources().getColor(R.color.textBlack));
//			}
//		}
//		if(list.get(position).getIsHide()){
//			viewHolder.accountNameTV.setVisibility(View.GONE);
//		}else{
//			viewHolder.accountNameTV.setVisibility(View.VISIBLE);
//		}
		if("1".equals(list.get(position).getAccountStatus())){
			viewHolder.settingBtn.setVisibility(View.VISIBLE);
			viewHolder.settingBtn.setText("设为已还");
			viewHolder.settingBtn.setTextColor(context.getResources().getColor(R.color.red));
			viewHolder.settingBtn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					callback.updateStatus(list.get(position));
				}
			});
		}else{
			viewHolder.settingBtn.setVisibility(View.GONE);
		}


		return convertView;
	}

	class ViewHolder {
		private ImageView prdIconIV = null;
		private TextView prdName = null;
		private TextView userName = null;
		private TextView amountTV = null;
		private TextView timeTV = null;
		private Button settingBtn = null;
		private TextView timeFlatTV = null;
		private TextView accountNameTV = null;
	}

}
